﻿using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;

namespace CustomReadFiles
{
    public class ReadClass
    {
        public IEnumerable<dynamic> ParseFile(string TxtFileName)
        {
            var retList = new List<dynamic>(); 
            var FileStream = OpenFile(TxtFileName);    
            if (FileStream != null)
            {
                string[] headerLine = FileStream.ReadLine().Split('》').Select(s => s.Trim()).ToArray();
                while (FileStream.Peek() > 0)
                {
                    string[] dataLine = FileStream.ReadLine().Split(',');
                    dynamic dynamicEntity = new ExpandoObject();

                    for (int i = 0; i < headerLine.Length; i++)
                    {
                        ((IDictionary<string, object>)dynamicEntity).Add(headerLine[i], dataLine[i]);
                    }
                    retList.Add(dynamicEntity);
                }
            }
            return retList;
        }
        private StreamReader OpenFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                return new StreamReader(File.OpenRead(fileName));
            }
            return null;
        }
    }
}
